package com.shaoyu.tools.testcase.event.async;

import com.alibaba.fastjson.JSON;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.shaoyu.tools.event.annotation.EventInterceptor;
import com.shaoyu.tools.event.core.EventBusExecutor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author shaoyu
 * @date 2022/11/22 10:38
 */
@Service
public class AsyncEventTestService implements InterfaceTestService{

    Logger logger = LoggerFactory.getLogger(AsyncEventTestService.class);

    /**
     * http://localhost:8888/test/asyncEventTestService/testSendEvent/%7B%22msg%22:%22hello%20world%22%7D
     */
    public String testSendEvent(AsyncTestEvent event){

        logger.info(this.getClass().getSimpleName()+"#"+"testSendEvent() 发送事件 AsyncTestEvent");

        EventBusExecutor.sendEvent(event);

        logger.info("事件已发送 AsyncTestEvent");
        return event.getMsg() + " at "+ sdf.format(new Date());
    }

    @EventInterceptor
    public boolean intercept1(AsyncTestEvent event) throws InterruptedException{
        logger.info("拦截器1 拦截 AsyncTestEvent 事件");
        if(StringUtils.isEmpty(event.getMsg())){
            Thread.sleep((long)(Math.random()*1000L));
            logger.info("AsyncTestEvent事件被拦截器1拦截");
            return false;
        }
        logger.info("AsyncTestEvent事件拦截器1通过");
        return true;
    }

    @EventInterceptor
    public boolean intercept2(AsyncTestEvent event) throws InterruptedException {
        logger.info("拦截器2 拦截 AsyncTestEvent 事件");
        if(StringUtils.isNotEmpty(event.getMsg())&& event.getMsg().contains("S")){
            Thread.sleep((long)(Math.random()*1000L));
            logger.info("AsyncTestEvent事件被拦截器2拦截");
            return false;
        }
        logger.info("AsyncTestEvent事件拦截器2通过");
        return true;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Subscribe
    public void subscribe1(AsyncTestEvent event){
        logger.info("订阅者1收到事件:"+ JSON.toJSONString(event)+ " at " + sdf.format(new Date()));
        try {
            Thread.sleep((long)(Math.random()*10000L));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("订阅者1处理完事件:"+ JSON.toJSONString(event)+ " at " + sdf.format(new Date()));
    }

    @Subscribe
    public void subscribe2(AsyncTestEvent event){
        logger.info("订阅者2收到事件:"+ JSON.toJSONString(event) + " at " + sdf.format(new Date()));
        try {
            Thread.sleep((long)(Math.random()*10000L));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(2-1==1){
            throw new RuntimeException("哎呀,异常了");
        }
        logger.info("订阅者2处理完事件:"+ JSON.toJSONString(event)+ " at " + sdf.format(new Date()));
    }


    @Subscribe
    @AllowConcurrentEvents
    public void subscribe3(AsyncTestEvent event){
        logger.info("订阅者3收到事件:"+ JSON.toJSONString(event) + " at " + sdf.format(new Date()));
        try {
            Thread.sleep((long)(Math.random()*10000L));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("订阅者3处理完事件:"+ JSON.toJSONString(event)+ " at " + sdf.format(new Date()));
    }

    @Subscribe
    @AllowConcurrentEvents
    @Override
    public void testInterface(AsyncTestEvent event) {
        logger.info("接口方法收到事件:"+ JSON.toJSONString(event) + " at " + sdf.format(new Date()));
    }
}
